<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Array 数组</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="language.types.numeric-strings.html">« 数字字符串</a></li>
      <li style="float: right;"><a href="language.types.object.html">Object 对象 »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="language.types.html">类型</a></li>
    <li>Array 数组</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="language.types.array" class="sect1">
 <h2 class="title">Array 数组</h2>

 <p class="para">
  PHP 中的 <span class="type">array</span> 实际上是一个有序映射。映射是一种把
  <em class="emphasis">values</em> 关联到 <em class="emphasis">keys</em> 的类型。此类型针对多种不同用途进行了优化；
  它可以被视为数组、列表（向量）、哈希表（映射的实现）、字典、集合、堆栈、队列等等。
  由于 <span class="type">array</span> 的值可以是其它 <span class="type">array</span>
  所以树形结构和多维 <span class="type">array</span> 也是允许的。
 </p>

 <p class="para">
  解释这些数据结构超出了本手册的范围，但对每种结构至少会提供一个例子。
  要得到这些数据结构的更多信息，建议参考有关此广阔主题的其它文献。
 </p>
   
 <div class="sect2" id="language.types.array.syntax">
  <h3 class="title">语法</h3>
  
  <div class="sect3" id="language.types.array.syntax.array-func">
   <h4 class="title">定义数组 <span class="function"><a href="function.array.html" class="function">array()</a></span></h4>

   <p class="para">
    可以用 <span class="function"><a href="function.array.html" class="function">array()</a></span> 语言结构来新建一个 <span class="type">array</span>。它接受任意数量用逗号分隔的
    <code class="literal"><span class="replaceable">键（key）</span> =&gt; <span class="replaceable">值（value）</span></code> 对。
   </p>

   <pre class="synopsis">
array(
    <span class="optional"><span class="replaceable">key</span>  =&gt; </span><span class="replaceable">value</span>,
    <span class="optional"><span class="replaceable">key2</span> =&gt; </span><span class="replaceable">value2</span>,
    <span class="optional"><span class="replaceable">key3</span> =&gt; </span><span class="replaceable">value3</span>,
    ...
)</pre>
   

   <p class="para">
    最后一个数组单元之后的逗号可以省略。通常用于单行数组定义中，例如常用
    <code class="literal">array(1, 2)</code> 而不是
    <code class="literal">array(1, 2, )</code>。对多行数组定义通常保留最后一个逗号，这样要添加一个新单元时更方便。
   </p>

   <blockquote class="note"><p><strong class="note">注意</strong>: 
    <p class="para">
     可以用短数组语法
     <code class="literal">[]</code> 替代 <code class="literal">array()</code> 。
    </p>
   </p></blockquote>

   <div class="example" id="example-54">
    <p><strong>示例 #1 一个简单数组</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$array&nbsp;</span><span style="color: #007700">=&nbsp;array(<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"foo"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"bar"</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"bar"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"foo"</span><span style="color: #007700">,<br />);<br /><br /></span><span style="color: #FF8000">//&nbsp;使用短数组语法<br /></span><span style="color: #0000BB">$array&nbsp;</span><span style="color: #007700">=&nbsp;[<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"foo"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"bar"</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"bar"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"foo"</span><span style="color: #007700">,<br />];<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>

   <p class="para">
    <span class="replaceable">key</span> 可以是 <span class="type">integer</span> 或者
    <span class="type">string</span>。<span class="replaceable">value</span> 可以是任意类型。
   </p>

   <p class="para" id="language.types.array.key-casts">
    此外 <span class="replaceable">key</span> 会有如下的强制转换：
    <ul class="itemizedlist">
     <li class="listitem">
      <span class="simpara">
       <span class="type">String</span> 中包含有效的十进制 <span class="type">int</span>，除非数字前面有一个 <code class="literal">+</code>
       号，否则将被转换为 <span class="type">int</span> 类型。例如键名 <code class="literal">&quot;8&quot;</code> 实际会被储存为
       <code class="literal">8</code>。另外， <code class="literal">&quot;08&quot;</code>
       不会被强制转换，因为它不是一个有效的十进制整数。
      </span>
     </li>
     <li class="listitem">
      <span class="simpara">
       <span class="type">Float</span> 也会被转换为 <span class="type">int</span> ，意味着其小数部分会被舍去。例如键名
       <code class="literal">8.7</code> 实际会被储存为 <code class="literal">8</code>。
      </span>
     </li>
     <li class="listitem">
      <span class="simpara">
       <span class="type">Bool</span> 也会被转换成 <span class="type">int</span>。即键名
       <code class="literal">true</code> 实际会被储存为 <code class="literal">1</code>
       而键名 <code class="literal">false</code> 会被储存为 <code class="literal">0</code>。
      </span>
     </li>
     <li class="listitem">
      <span class="simpara">
       <span class="type">Null</span> 会被转换为空字符串，即键名
       <code class="literal">null</code> 实际会被储存为 <code class="literal">&quot;&quot;</code>。
      </span>
     </li>
     <li class="listitem">
      <span class="simpara">
       <span class="type">Array</span> 和 <span class="type">object</span> <em class="emphasis">不能</em>
       被用为键名。坚持这么做会导致警告：<code class="literal">Illegal offset type</code>。
      </span>
     </li>
    </ul>
   </p>
   
   <p class="para">
    如果在数组定义时多个元素都使用相同键名，那么只有最后一个会被使用，其它的元素都会被覆盖。
   </p>

   <div class="example" id="example-55">
    <p><strong>示例 #2 类型转换与覆盖的示例</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$array&nbsp;</span><span style="color: #007700">=&nbsp;array(<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">1&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"a"</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"1"&nbsp;&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"b"</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">1.5&nbsp;&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"c"</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">true&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"d"</span><span style="color: #007700">,<br />);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$array</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
array(1) {
  [1]=&gt;
  string(1) &quot;d&quot;
}
</pre></div>
    </div>
    <div class="example-contents"><p>
     上例中所有的键名都被强制转换为 <code class="literal">1</code>，则每一个新单元都会覆盖前一个的值，最后剩下的只有一个
     <code class="literal">&quot;d&quot;</code>。
    </p></div>
   </div>  

   <p class="para">
    PHP 数组可以同时含有 <span class="type">int</span> 和 <span class="type">string</span> 类型的键名，因为
    PHP 实际并不区分索引数组和关联数组。
   </p>
   
   <div class="example" id="example-56">
    <p><strong>示例 #3 混合 <span class="type">int</span> 和 <span class="type">string</span> 键名</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$array&nbsp;</span><span style="color: #007700">=&nbsp;array(<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"foo"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"bar"</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"bar"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"foo"</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">100&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">=&gt;&nbsp;-</span><span style="color: #0000BB">100</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;-</span><span style="color: #0000BB">100&nbsp;&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">100</span><span style="color: #007700">,<br />);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$array</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
array(4) {
  [&quot;foo&quot;]=&gt;
  string(3) &quot;bar&quot;
  [&quot;bar&quot;]=&gt;
  string(3) &quot;foo&quot;
  [100]=&gt;
  int(-100)
  [-100]=&gt;
  int(100)
}
</pre></div>
    </div>
   </div>
   
   <p class="para">
    <span class="replaceable">key</span> 为可选项。如果未指定，PHP
    将自动使用之前用过的最大 <span class="type">int</span> 键名加上 1 作为新的键名。
   </p>
   
   <div class="example" id="example-57">
    <p><strong>示例 #4 没有键名的索引数组</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$array&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #DD0000">"foo"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"bar"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"hello"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"world"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$array</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
array(4) {
  [0]=&gt;
  string(3) &quot;foo&quot;
  [1]=&gt;
  string(3) &quot;bar&quot;
  [2]=&gt;
  string(5) &quot;hello&quot;
  [3]=&gt;
  string(5) &quot;world&quot;
}
</pre></div>
    </div>
   </div>
   
   <p class="para">
    还可以只对某些单元指定键名而对其它的空置：
   </p>
   
   <div class="example" id="example-58">
    <p><strong>示例 #5 仅对部分单元指定键名</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$array&nbsp;</span><span style="color: #007700">=&nbsp;array(<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"a"</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"b"</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">6&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"c"</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"d"</span><span style="color: #007700">,<br />);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$array</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
array(4) {
  [0]=&gt;
  string(1) &quot;a&quot;
  [1]=&gt;
  string(1) &quot;b&quot;
  [6]=&gt;
  string(1) &quot;c&quot;
  [7]=&gt;
  string(1) &quot;d&quot;
}
</pre></div>
    </div>
    <div class="example-contents"><p>
     可以看到最后一个值 <code class="literal">&quot;d&quot;</code> 被自动赋予了键名
     <code class="literal">7</code>。这是由于之前最大的整数键名是 <code class="literal">6</code>。
    </p></div>
   </div>

   <div class="example" id="example-59">
    <p><strong>示例 #6 复杂类型转换和覆盖的例子</strong></p>
    <div class="example-contents"><p>
     这个例子包括键名类型转换和元素覆盖的所有变化。
    </p></div>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$array&nbsp;</span><span style="color: #007700">=&nbsp;array(<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">1&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'a'</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'1'&nbsp;&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'b'</span><span style="color: #007700">,&nbsp;</span><span style="color: #FF8000">//&nbsp;值&nbsp;"a"&nbsp;会被&nbsp;"b"&nbsp;覆盖<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">1.5&nbsp;&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'c'</span><span style="color: #007700">,&nbsp;</span><span style="color: #FF8000">//&nbsp;值&nbsp;"b"&nbsp;会被&nbsp;"c"&nbsp;覆盖<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">-</span><span style="color: #0000BB">1&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'d'</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'01'&nbsp;&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'e'</span><span style="color: #007700">,&nbsp;</span><span style="color: #FF8000">//&nbsp;由于这不是整数字符串，因此不会覆盖键名&nbsp;1<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'1.5'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'f'</span><span style="color: #007700">,&nbsp;</span><span style="color: #FF8000">//&nbsp;由于这不是整数字符串，因此不会覆盖键名&nbsp;1<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">true&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'g'</span><span style="color: #007700">,&nbsp;</span><span style="color: #FF8000">//&nbsp;值&nbsp;"c"&nbsp;会被&nbsp;"g"&nbsp;覆盖<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">false&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'h'</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">''&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'i'</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">null&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'j'</span><span style="color: #007700">,&nbsp;</span><span style="color: #FF8000">//&nbsp;值&nbsp;"i"&nbsp;会被&nbsp;"j"&nbsp;覆盖<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'k'</span><span style="color: #007700">,&nbsp;</span><span style="color: #FF8000">//&nbsp;值&nbsp;“k”&nbsp;的键名被分配为&nbsp;2。这是因为之前最大的整数键是&nbsp;1<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">2&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'l'</span><span style="color: #007700">,&nbsp;</span><span style="color: #FF8000">//&nbsp;值&nbsp;"k"&nbsp;会被&nbsp;"l"&nbsp;覆盖<br /></span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$array</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
array(7) {
  [1]=&gt;
  string(1) &quot;g&quot;
  [-1]=&gt;
  string(1) &quot;d&quot;
  [&quot;01&quot;]=&gt;
  string(1) &quot;e&quot;
  [&quot;1.5&quot;]=&gt;
  string(1) &quot;f&quot;
  [0]=&gt;
  string(1) &quot;h&quot;
  [&quot;&quot;]=&gt;
  string(1) &quot;j&quot;
  [2]=&gt;
  string(1) &quot;l&quot;
}
</pre></div>
    </div>
   </div>
  </div>
  
  <div class="sect3" id="language.types.array.syntax.accessing">
   <h4 class="title">用方括号语法访问数组单元</h4>
   
   <p class="para">
    数组单元可以通过 <code class="literal">array[key]</code> 语法来访问。
   </p>
   
   <div class="example" id="example-60">
    <p><strong>示例 #7 访问数组单元</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$array&nbsp;</span><span style="color: #007700">=&nbsp;array(<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"foo"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"bar"</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">42&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">24</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"multi"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;array(<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"dimensional"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;array(<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"array"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"foo"<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">)<br />&nbsp;&nbsp;&nbsp;&nbsp;)<br />);<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$array</span><span style="color: #007700">[</span><span style="color: #DD0000">"foo"</span><span style="color: #007700">]);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$array</span><span style="color: #007700">[</span><span style="color: #0000BB">42</span><span style="color: #007700">]);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$array</span><span style="color: #007700">[</span><span style="color: #DD0000">"multi"</span><span style="color: #007700">][</span><span style="color: #DD0000">"dimensional"</span><span style="color: #007700">][</span><span style="color: #DD0000">"array"</span><span style="color: #007700">]);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
string(3) &quot;bar&quot;
int(24)
string(3) &quot;foo&quot;
</pre></div>
    </div>
   </div>
   
   <blockquote class="note"><p><strong class="note">注意</strong>: 
    <p class="para">
     在 PHP 8.0.0 之前，方括号和花括号可以互换使用来访问数组单元（例如
     <code class="literal">$array[42]</code> 和 <code class="literal">$array{42}</code>
     在上例中效果相同）。 花括号语法在 PHP 7.4.0 中已弃用，在 PHP 8.0.0 中不再支持。
    </p>
   </p></blockquote>

   <div class="example" id="example-61">
    <p><strong>示例 #8 数组解引用</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">getArray</span><span style="color: #007700">()&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;array(</span><span style="color: #0000BB">1</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">3</span><span style="color: #007700">);<br />}<br /><br /></span><span style="color: #0000BB">$secondElement&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">getArray</span><span style="color: #007700">()[</span><span style="color: #0000BB">1</span><span style="color: #007700">];<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
   
   <blockquote class="note"><p><strong class="note">注意</strong>: 
    <p class="para">
     试图访问一个未定义的数组键名与访问任何未定义变量一样：会导致
     <strong><code>E_WARNING</code></strong>
     级别错误信息（在 PHP 8.0.0 之前是 <strong><code>E_NOTICE</code></strong>
     级别），其结果为 <strong><code>null</code></strong>。
    </p>
   </p></blockquote>
   <blockquote class="note"><p><strong class="note">注意</strong>: 
    <p class="para">
     数组解引用非 <span class="type">string</span> 的标量值会产生 <strong><code>null</code></strong>。
     在 PHP 7.4.0 之前，它不会发出错误消息。
     从 PHP 7.4.0 开始，这个问题产生 <strong><code>E_NOTICE</code></strong> ；
     从 PHP 8.0.0 开始，这个问题产生 <strong><code>E_WARNING</code></strong> 。
    </p>
   </p></blockquote>
  </div>

  <div class="sect3" id="language.types.array.syntax.modifying">
   <h4 class="title">用方括号的语法新建／修改</h4>

   <p class="para">
    可以通过明示地设定其中的值来修改现有的 <span class="type">array</span> 。
   </p>

   <p class="para">
    这是通过在方括号内指定键名来给 <span class="type">array</span>
    赋值实现的。也可以省略键名，在这种情况下给变量名加上一对空的方括号（<code class="literal">[]</code>）。
   </p>
   
   <pre class="synopsis">
$arr[<span class="replaceable">key</span>] = <span class="replaceable">value</span>;
$arr[] = <span class="replaceable">value</span>;
// <span class="replaceable">key</span> 可以是 <span class="type">int</span> 或 <span class="type">string</span>
// <span class="replaceable">value</span> 可以是任意类型的值</pre>
   
   <p class="para">
    如果 <var class="varname">$arr</var> 不存在或者设置为 <strong><code>null</code></strong> 或者 <strong><code>false</code></strong>，将会新建它，这也是另一种创建
    <span class="type">array</span> 的方法。不过并不鼓励这样做，因为如果
    <var class="varname">$arr</var> 已经包含有值（例如来自请求变量的 <span class="type">string</span>）则此值会保留而
    <code class="literal">[]</code> 实际上代表着<a href="language.types.string.html#language.types.string.substr" class="link">字符串访问运算符</a>。初始化变量的最好方式是直接给其赋值。
   </p>
   <blockquote class="note"><p><strong class="note">注意</strong>: 
    <span class="simpara">
     从 PHP 7.1.0 起，对字符串应用空索引操作符会抛出致命错误。以前，字符串被静默地转换为数组。
    </span>
   </p></blockquote>
   <blockquote class="note"><p><strong class="note">注意</strong>: 
    <span class="simpara">
     从 PHP 8.1.0 起，弃用从 <strong><code>false</code></strong> 值中创建一个新数组。
     但仍然允许从 <strong><code>null</code></strong> 或者未定义的变量中创建新数组。
    </span>
   </p></blockquote>

   <p class="para">
    要修改某个值，通过其键名给该单元赋一个新值。要删除某键值对，对其调用
    <span class="function"><a href="function.unset.html" class="function">unset()</a></span> 函数。
   </p>

   <div class="informalexample">
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$arr&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #0000BB">5&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">12&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$arr</span><span style="color: #007700">[]&nbsp;=&nbsp;</span><span style="color: #0000BB">56</span><span style="color: #007700">;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;这与&nbsp;$arr[13]&nbsp;=&nbsp;56&nbsp;相同;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;在脚本的这一点上<br /><br /></span><span style="color: #0000BB">$arr</span><span style="color: #007700">[</span><span style="color: #DD0000">"x"</span><span style="color: #007700">]&nbsp;=&nbsp;</span><span style="color: #0000BB">42</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;添加一个新元素<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;键名使用&nbsp;"x"<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br /></span><span style="color: #007700">unset(</span><span style="color: #0000BB">$arr</span><span style="color: #007700">[</span><span style="color: #0000BB">5</span><span style="color: #007700">]);&nbsp;</span><span style="color: #FF8000">//&nbsp;从数组中删除元素<br /><br /></span><span style="color: #007700">unset(</span><span style="color: #0000BB">$arr</span><span style="color: #007700">);&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;删除整个数组<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div> 
    </div>

   </div>

   <blockquote class="note"><p><strong class="note">注意</strong>: 
    <p class="para">
     如上所述，如果给出方括号但没有指定键名，则取当前最大
     <span class="type">int</span> 索引值，新的键名将是该值加上
     1（但是最小为 0）。如果当前还没有 <span class="type">int</span> 索引，则键名将为
     <code class="literal">0</code> 。
    </p>

    <p class="para">
     注意这里所使用的最大整数键名<em class="emphasis">目前不需要存在于 <span class="type">array</span>
     中</em>。 它只要在上次 <span class="type">array</span> 重新生成索引后曾经存在于
     <span class="type">array</span> 就行了。以下面的例子来说明：
    </p>

    <div class="informalexample">
     <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;创建一个简单的数组<br /></span><span style="color: #0000BB">$array&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #0000BB">1</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">3</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">4</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">5</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$array</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;现在删除其中的所有元素，但保持数组本身不变:<br /></span><span style="color: #007700">foreach&nbsp;(</span><span style="color: #0000BB">$array&nbsp;</span><span style="color: #007700">as&nbsp;</span><span style="color: #0000BB">$i&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">$value</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;unset(</span><span style="color: #0000BB">$array</span><span style="color: #007700">[</span><span style="color: #0000BB">$i</span><span style="color: #007700">]);<br />}<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$array</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;添加一个单元（注意新的键名是&nbsp;5，而不是你可能以为的&nbsp;0）<br /></span><span style="color: #0000BB">$array</span><span style="color: #007700">[]&nbsp;=&nbsp;</span><span style="color: #0000BB">6</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$array</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;重新索引：<br /></span><span style="color: #0000BB">$array&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">array_values</span><span style="color: #007700">(</span><span style="color: #0000BB">$array</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$array</span><span style="color: #007700">[]&nbsp;=&nbsp;</span><span style="color: #0000BB">7</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$array</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div> 
     </div>

     <p class="para">以上例程会输出：</p>
     <div class="example-contents screen">
<div class="cdata"><pre>
Array
(
    [0] =&gt; 1
    [1] =&gt; 2
    [2] =&gt; 3
    [3] =&gt; 4
    [4] =&gt; 5
)
Array
(
)
Array
(
    [5] =&gt; 6
)
Array
(
    [0] =&gt; 6
    [1] =&gt; 7
)
</pre></div>
     </div>
    </div>

   </p></blockquote>

  </div>

  <div class="sect3" id="language.types.array.syntax.destructuring">
   <h4 class="title">数组解包</h4>

   <p class="para">
    可以使用 <code class="literal">[]</code>（自 PHP 7.1.0 起）或者 <span class="function"><a href="function.list.html" class="function">list()</a></span>
    语言结构解包数组。这些结构可用于将数组解包为不同的变量。
   </p>

   <div class="informalexample">
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$source_array&nbsp;</span><span style="color: #007700">=&nbsp;[</span><span style="color: #DD0000">'foo'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'bar'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'baz'</span><span style="color: #007700">];<br /><br />[</span><span style="color: #0000BB">$foo</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$bar</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$baz</span><span style="color: #007700">]&nbsp;=&nbsp;</span><span style="color: #0000BB">$source_array</span><span style="color: #007700">;<br /><br />echo&nbsp;</span><span style="color: #0000BB">$foo</span><span style="color: #007700">;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;打印&nbsp;"foo"<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">$bar</span><span style="color: #007700">;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;打印&nbsp;"bar"<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">$baz</span><span style="color: #007700">;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;打印&nbsp;"baz"<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>

   <p class="para">
    数组解包可用于 <a href="control-structures.foreach.html" class="link">foreach</a> 在迭代多维数组时对其进行解包。
   </p>

   <div class="informalexample">
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$source_array&nbsp;</span><span style="color: #007700">=&nbsp;[<br />&nbsp;&nbsp;&nbsp;&nbsp;[</span><span style="color: #0000BB">1</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'John'</span><span style="color: #007700">],<br />&nbsp;&nbsp;&nbsp;&nbsp;[</span><span style="color: #0000BB">2</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'Jane'</span><span style="color: #007700">],<br />];<br /><br />foreach&nbsp;(</span><span style="color: #0000BB">$source_array&nbsp;</span><span style="color: #007700">as&nbsp;[</span><span style="color: #0000BB">$id</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$name</span><span style="color: #007700">])&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;这里是&nbsp;$id&nbsp;和&nbsp;$name&nbsp;的逻辑<br /></span><span style="color: #007700">}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>

   <p class="para">
    如果变量未提供，数组元素将会被忽略。数组解包始终从索引 <code class="literal">0</code> 开始。
   </p>

   <div class="informalexample">
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$source_array&nbsp;</span><span style="color: #007700">=&nbsp;[</span><span style="color: #DD0000">'foo'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'bar'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'baz'</span><span style="color: #007700">];<br /><br /></span><span style="color: #FF8000">//&nbsp;将索引&nbsp;2&nbsp;的元素分配给变量&nbsp;$baz<br /></span><span style="color: #007700">[,&nbsp;,&nbsp;</span><span style="color: #0000BB">$baz</span><span style="color: #007700">]&nbsp;=&nbsp;</span><span style="color: #0000BB">$source_array</span><span style="color: #007700">;<br /><br />echo&nbsp;</span><span style="color: #0000BB">$baz</span><span style="color: #007700">;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;打印&nbsp;"baz"<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>

   <p class="para">
    自 PHP 7.1.0 起，也可以解包关联数组。这在数字索引数组中更容易选择正确的元素，因为可以显式指定索引。
   </p>

   <div class="informalexample">
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$source_array&nbsp;</span><span style="color: #007700">=&nbsp;[</span><span style="color: #DD0000">'foo'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'bar'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'baz'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">3</span><span style="color: #007700">];<br /><br /></span><span style="color: #FF8000">//&nbsp;将索引&nbsp;'baz'&nbsp;处的元素分配给变量&nbsp;$three<br /></span><span style="color: #007700">[</span><span style="color: #DD0000">'baz'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">$three</span><span style="color: #007700">]&nbsp;=&nbsp;</span><span style="color: #0000BB">$source_array</span><span style="color: #007700">;<br /><br />echo&nbsp;</span><span style="color: #0000BB">$three</span><span style="color: #007700">;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;打印&nbsp;3<br /><br /></span><span style="color: #0000BB">$source_array&nbsp;</span><span style="color: #007700">=&nbsp;[</span><span style="color: #DD0000">'foo'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'bar'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'baz'</span><span style="color: #007700">];<br /><br /></span><span style="color: #FF8000">//&nbsp;将索引&nbsp;2&nbsp;处的元素分配给变量&nbsp;$baz<br /></span><span style="color: #007700">[</span><span style="color: #0000BB">2&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">$baz</span><span style="color: #007700">]&nbsp;=&nbsp;</span><span style="color: #0000BB">$source_array</span><span style="color: #007700">;<br /><br />echo&nbsp;</span><span style="color: #0000BB">$baz</span><span style="color: #007700">;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;打印&nbsp;"baz"<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>

   <p class="para">
    数组解包可以方便的用于两个变量交换。
   </p>

   <div class="informalexample">
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$a&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$b&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">;<br /><br />[</span><span style="color: #0000BB">$b</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$a</span><span style="color: #007700">]&nbsp;=&nbsp;[</span><span style="color: #0000BB">$a</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$b</span><span style="color: #007700">];<br /><br />echo&nbsp;</span><span style="color: #0000BB">$a</span><span style="color: #007700">;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;打印&nbsp;2<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">$b</span><span style="color: #007700">;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;打印&nbsp;1<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>

   <blockquote class="note"><p><strong class="note">注意</strong>: 
    <p class="para">
      分配时不支持展开运算符（<code class="literal">...</code>）。
    </p>
   </p></blockquote>

   <blockquote class="note"><p><strong class="note">注意</strong>: 
    <p class="para">
      尝试访问未定义的数组键与访问任何未定义的变量相同：都将发出 <strong><code>E_WARNING</code></strong>
      级别的错误消息（PHP 8.0.0 之前是 <strong><code>E_NOTICE</code></strong> 级别），结果将是 <strong><code>null</code></strong>。
    </p>
   </p></blockquote>
  </div>

 </div>
 
 <div class="sect2" id="language.types.array.useful-funcs">
  <h3 class="title">实用函数</h3>

  <p class="para">
    有很多操作数组的函数，参见 <a href="ref.array.html" class="link">数组函数</a> 一节。
  </p>

  <blockquote class="note"><p><strong class="note">注意</strong>: 
   <p class="para">
    <span class="function"><a href="function.unset.html" class="function">unset()</a></span> 函数允许删除
    <span class="type">array</span> 中的某个键。但要注意数组将<em class="emphasis">不会</em>重建索引。如果需要删除后重建索引，可以用
    <span class="function"><a href="function.array-values.html" class="function">array_values()</a></span> 函数重建 <span class="type">array</span> 索引。
   </p>
   
   <div class="informalexample">
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$a&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #0000BB">1&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'one'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">2&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'two'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">3&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'three'</span><span style="color: #007700">);<br />unset(</span><span style="color: #0000BB">$a</span><span style="color: #007700">[</span><span style="color: #0000BB">2</span><span style="color: #007700">]);<br /></span><span style="color: #FF8000">/*&nbsp;该数组将被定义为<br />&nbsp;&nbsp;&nbsp;$a&nbsp;=&nbsp;array(1&nbsp;=&gt;&nbsp;'one',&nbsp;3&nbsp;=&gt;&nbsp;'three');<br />&nbsp;&nbsp;&nbsp;而不是<br />&nbsp;&nbsp;&nbsp;$a&nbsp;=&nbsp;array(1&nbsp;=&gt;&nbsp;'one',&nbsp;2&nbsp;=&gt;'three');<br />*/<br /><br /></span><span style="color: #0000BB">$b&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">array_values</span><span style="color: #007700">(</span><span style="color: #0000BB">$a</span><span style="color: #007700">);<br /></span><span style="color: #FF8000">//&nbsp;现在&nbsp;$b&nbsp;是&nbsp;array(0&nbsp;=&gt;&nbsp;'one',&nbsp;1&nbsp;=&gt;'three')<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p></blockquote> 

  <p class="para">
   <a href="control-structures.foreach.html" class="link">foreach</a> 控制结构是专门用于
   <span class="type">array</span> 的。它提供了一个简单的方法来遍历 <span class="type">array</span>。
  </p>
 </div>
 
 <div class="sect2" id="language.types.array.donts">
  <h3 class="title">数组做什么和不做什么</h3>

  <div class="sect3" id="language.types.array.foo-bar">
   <h4 class="title">为什么 <code class="literal">$foo[bar]</code> 错了？</h4>

   <p class="para">
     应该始终在用字符串表示的数组索引上加上引号。例如用
    <code class="literal">$foo[&#039;bar&#039;]</code> 而不是
    <code class="literal">$foo[bar]</code>。但是为什么呢？可能在老的脚本中见过如下语法：
   </p>

   <div class="informalexample">
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$foo</span><span style="color: #007700">[</span><span style="color: #0000BB">bar</span><span style="color: #007700">]&nbsp;=&nbsp;</span><span style="color: #DD0000">'enemy'</span><span style="color: #007700">;<br />echo&nbsp;</span><span style="color: #0000BB">$foo</span><span style="color: #007700">[</span><span style="color: #0000BB">bar</span><span style="color: #007700">];<br /></span><span style="color: #FF8000">//&nbsp;及其它<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
   
   <p class="para">
    这样是错的，但可以正常运行。那么为什么错了呢？原因是此代码中有一个未定义的常量（
    <code class="literal">bar</code> ）而不是 <span class="type">string</span>（<code class="literal">&#039;bar&#039;</code>－注意引号）。而
    PHP 可能会在以后定义此常量，不幸的是你的代码中有同样的名字。它能运行，是因为
    PHP 自动将<em class="emphasis">裸字符串</em>（没有引号的 <span class="type">string</span>
    且不对应于任何已知符号）转换成一个其值为该裸 <span class="type">string</span> 的
    <span class="type">string</span>。例如，如果没有常量定义为
    <strong><code>bar</code></strong>，那么PHP 将在
    <span class="type">string</span> 中替代为 <code class="literal">&#039;bar&#039;</code> 并使用之。
   </p>
   <div class="warning"><strong class="warning">警告</strong>
    <p class="simpara">
     将未定义的常量当作裸字符串的回退会触发 <strong><code>E_NOTICE</code></strong> 级别错误。
     从 PHP 7.2.0 起已废弃，并触发 <strong><code>E_WARNING</code></strong> 级别错误。
     从 PHP 8.0.0 起被移除，并触发 <span class="classname"><a href="class.error.html" class="classname">Error</a></span> 异常。
    </p>
   </div>

   <blockquote class="note"><p><strong class="note">注意</strong>: 
    <span class="simpara">
     这并不意味着<em class="emphasis">总是</em>给键名加上引号。用不着给键名为
     <a href="language.constants.html" class="link">常量</a> 或
     <a href="language.variables.html" class="link">变量</a> 的加上引号，否则会使 PHP
     不能解析它们。
    </span>

    <div class="informalexample">
     <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />error_reporting</span><span style="color: #007700">(</span><span style="color: #0000BB">E_ALL</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">ini_set</span><span style="color: #007700">(</span><span style="color: #DD0000">'display_errors'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">true</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">ini_set</span><span style="color: #007700">(</span><span style="color: #DD0000">'html_errors'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">false</span><span style="color: #007700">);<br /></span><span style="color: #FF8000">//&nbsp;简单数组:<br /></span><span style="color: #0000BB">$array&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #0000BB">1</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$count&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">count</span><span style="color: #007700">(</span><span style="color: #0000BB">$array</span><span style="color: #007700">);<br />for&nbsp;(</span><span style="color: #0000BB">$i&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">;&nbsp;</span><span style="color: #0000BB">$i&nbsp;</span><span style="color: #007700">&lt;&nbsp;</span><span style="color: #0000BB">$count</span><span style="color: #007700">;&nbsp;</span><span style="color: #0000BB">$i</span><span style="color: #007700">++)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"\n检查&nbsp;</span><span style="color: #0000BB">$i</span><span style="color: #DD0000">:&nbsp;\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"坏的:&nbsp;"&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">$array</span><span style="color: #007700">[</span><span style="color: #DD0000">'$i'</span><span style="color: #007700">]&nbsp;.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"好的:&nbsp;"&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">$array</span><span style="color: #007700">[</span><span style="color: #0000BB">$i</span><span style="color: #007700">]&nbsp;.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"坏的:&nbsp;</span><span style="color: #007700">{</span><span style="color: #0000BB">$array</span><span style="color: #007700">[</span><span style="color: #DD0000">'$i'</span><span style="color: #007700">]}</span><span style="color: #DD0000">\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"好的:&nbsp;</span><span style="color: #007700">{</span><span style="color: #0000BB">$array</span><span style="color: #007700">[</span><span style="color: #0000BB">$i</span><span style="color: #007700">]}</span><span style="color: #DD0000">\n"</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
     </div>

    </div>
    <p class="para">以上例程会输出：</p>
    <div class="example-contents screen">
<div class="cdata"><pre>
检查 0:
Notice: Undefined index:  $i in /path/to/script.html on line 9
坏的:
好的: 1
Notice: Undefined index:  $i in /path/to/script.html on line 11
坏的:
好的: 1

检查 1:
Notice: Undefined index:  $i in /path/to/script.html on line 9
坏的:
好的: 2
Notice: Undefined index:  $i in /path/to/script.html on line 11
坏的:
好的: 2
</pre></div>        
    </div>
   </p></blockquote>

   <p class="para">
    演示此行为的更多例子：
   </p>

   <div class="informalexample">
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;显示所有错误<br /></span><span style="color: #0000BB">error_reporting</span><span style="color: #007700">(</span><span style="color: #0000BB">E_ALL</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$arr&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #DD0000">'fruit'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'apple'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'veggie'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'carrot'</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;正确的<br /></span><span style="color: #007700">print&nbsp;</span><span style="color: #0000BB">$arr</span><span style="color: #007700">[</span><span style="color: #DD0000">'fruit'</span><span style="color: #007700">];&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;apple<br /></span><span style="color: #007700">print&nbsp;</span><span style="color: #0000BB">$arr</span><span style="color: #007700">[</span><span style="color: #DD0000">'veggie'</span><span style="color: #007700">];&nbsp;</span><span style="color: #FF8000">//&nbsp;carrot<br /><br />//&nbsp;不正确的。&nbsp;&nbsp;这可以工作，但也会抛出一个&nbsp;E_NOTICE&nbsp;级别的&nbsp;PHP&nbsp;错误，因为<br />//&nbsp;未定义名为&nbsp;apple&nbsp;的常量<br />//&nbsp;<br />//&nbsp;Notice:&nbsp;Use&nbsp;of&nbsp;undefined&nbsp;constant&nbsp;fruit&nbsp;-&nbsp;assumed&nbsp;'fruit'&nbsp;in...<br /></span><span style="color: #007700">print&nbsp;</span><span style="color: #0000BB">$arr</span><span style="color: #007700">[</span><span style="color: #0000BB">fruit</span><span style="color: #007700">];&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;apple<br /><br />//&nbsp;这定义了一个常量来演示正在发生的事情。&nbsp;&nbsp;值&nbsp;'veggie'<br />//&nbsp;被分配给一个名为&nbsp;fruit&nbsp;的常量。<br /></span><span style="color: #0000BB">define</span><span style="color: #007700">(</span><span style="color: #DD0000">'fruit'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'veggie'</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;注意这里的区别<br /></span><span style="color: #007700">print&nbsp;</span><span style="color: #0000BB">$arr</span><span style="color: #007700">[</span><span style="color: #DD0000">'fruit'</span><span style="color: #007700">];&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;apple<br /></span><span style="color: #007700">print&nbsp;</span><span style="color: #0000BB">$arr</span><span style="color: #007700">[</span><span style="color: #0000BB">fruit</span><span style="color: #007700">];&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;carrot<br /><br />//&nbsp;以下是可以的，因为它在字符串中。<br />//&nbsp;不会在字符串中查找常量，因此此处不会出现&nbsp;E_NOTICE<br /></span><span style="color: #007700">print&nbsp;</span><span style="color: #DD0000">"Hello&nbsp;</span><span style="color: #0000BB">$arr</span><span style="color: #007700">[</span><span style="color: #0000BB">fruit</span><span style="color: #007700">]</span><span style="color: #DD0000">"</span><span style="color: #007700">;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;Hello&nbsp;apple<br /><br />//&nbsp;有一个例外：字符串中花括号围绕的数组中常量可以被解释<br />//<br /></span><span style="color: #007700">print&nbsp;</span><span style="color: #DD0000">"Hello&nbsp;</span><span style="color: #007700">{</span><span style="color: #0000BB">$arr</span><span style="color: #007700">[</span><span style="color: #0000BB">fruit</span><span style="color: #007700">]}</span><span style="color: #DD0000">"</span><span style="color: #007700">;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;Hello&nbsp;carrot<br /></span><span style="color: #007700">print&nbsp;</span><span style="color: #DD0000">"Hello&nbsp;</span><span style="color: #007700">{</span><span style="color: #0000BB">$arr</span><span style="color: #007700">[</span><span style="color: #DD0000">'fruit'</span><span style="color: #007700">]}</span><span style="color: #DD0000">"</span><span style="color: #007700">;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;Hello&nbsp;apple<br /><br />//&nbsp;这将不起作用，并会导致解析错误，例如：<br />//&nbsp;Parse&nbsp;error:&nbsp;parse&nbsp;error,&nbsp;expecting&nbsp;T_STRING'&nbsp;or&nbsp;T_VARIABLE'&nbsp;or&nbsp;T_NUM_STRING'<br />//&nbsp;这当然也适用于在字符串中使用超全局变量<br /></span><span style="color: #007700">print&nbsp;</span><span style="color: #DD0000">"Hello&nbsp;</span><span style="color: #0000BB">$arr</span><span style="color: #007700">[</span><span style="color: #DD0000">'fruit']"</span><span style="color: #007700">;<br />print&nbsp;</span><span style="color: #DD0000">"Hello&nbsp;</span><span style="color: #0000BB">$_GET</span><span style="color: #007700">[</span><span style="color: #DD0000">'foo']"</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">//&nbsp;串联是另一种选择<br /></span><span style="color: #007700">print&nbsp;</span><span style="color: #DD0000">"Hello&nbsp;"&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">$arr</span><span style="color: #007700">[</span><span style="color: #DD0000">'fruit'</span><span style="color: #007700">];&nbsp;</span><span style="color: #FF8000">//&nbsp;Hello&nbsp;apple<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>

   <p class="para">
    当打开 <a href="errorfunc.configuration.html#ini.error-reporting" class="link">error_reporting</a> 来显示
    <strong><code>E_NOTICE</code></strong> 级别的错误（将其设为
    <strong><code>E_ALL</code></strong>）时将看到这些错误。默认情况下
    <a href="errorfunc.configuration.html#ini.error-reporting" class="link">error_reporting</a>
    被关闭不显示这些。
   </p>

   <p class="para">
    和在 <a href="language.types.array.html#language.types.array.syntax" class="link">语法</a>
    一节中规定的一样，在方括号（<code class="literal">[</code>” 和
    “<code class="literal">]</code>”）之间必须有一个表达式。这意味着可以这样写：
   </p>

   <div class="informalexample">
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">$arr</span><span style="color: #007700">[</span><span style="color: #0000BB">somefunc</span><span style="color: #007700">(</span><span style="color: #0000BB">$bar</span><span style="color: #007700">)];<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
   
   <p class="para">
    这是一个用函数返回值作为数组索引的例子。PHP 也可以用已知常量，可能之前已经见过：
   </p>

   <div class="informalexample">
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$error_descriptions</span><span style="color: #007700">[</span><span style="color: #0000BB">E_ERROR</span><span style="color: #007700">]&nbsp;&nbsp;&nbsp;=&nbsp;</span><span style="color: #DD0000">"A&nbsp;fatal&nbsp;error&nbsp;has&nbsp;occurred"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$error_descriptions</span><span style="color: #007700">[</span><span style="color: #0000BB">E_WARNING</span><span style="color: #007700">]&nbsp;=&nbsp;</span><span style="color: #DD0000">"PHP&nbsp;issued&nbsp;a&nbsp;warning"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$error_descriptions</span><span style="color: #007700">[</span><span style="color: #0000BB">E_NOTICE</span><span style="color: #007700">]&nbsp;&nbsp;=&nbsp;</span><span style="color: #DD0000">"This&nbsp;is&nbsp;just&nbsp;an&nbsp;informal&nbsp;notice"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
   
   <p class="para">
    注意 <code class="literal">E_ERROR</code> 也是个合法的标识符，就和第一个例子中的
    <code class="literal">bar</code> 一样。但是上一个例子实际上和如下写法是一样的：
   </p>
  
   <div class="informalexample">
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$error_descriptions</span><span style="color: #007700">[</span><span style="color: #0000BB">1</span><span style="color: #007700">]&nbsp;=&nbsp;</span><span style="color: #DD0000">"A&nbsp;fatal&nbsp;error&nbsp;has&nbsp;occurred"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$error_descriptions</span><span style="color: #007700">[</span><span style="color: #0000BB">2</span><span style="color: #007700">]&nbsp;=&nbsp;</span><span style="color: #DD0000">"PHP&nbsp;issued&nbsp;a&nbsp;warning"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$error_descriptions</span><span style="color: #007700">[</span><span style="color: #0000BB">8</span><span style="color: #007700">]&nbsp;=&nbsp;</span><span style="color: #DD0000">"This&nbsp;is&nbsp;just&nbsp;an&nbsp;informal&nbsp;notice"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
   
   <p class="para">
    因为 <code class="literal">E_ERROR</code> 等于 <code class="literal">1</code>，等等。
   </p>

   <div class="sect4" id="language.types.array.foo-bar.why">
    <h5 class="title">那么为什么这样做不好？</h5>

    <p class="para">
     也许有一天，PHP 开发小组可能会想新增一个常量或者关键字，或者用户可能希望以后在自己的程序中引入新的常量，那就有麻烦了。例如已经不能这样用
     <code class="literal">empty</code> 和 <code class="literal">default</code> 这两个词了，因为他们是
     <a href="reserved.html" class="link">保留关键字</a>。
    </p>

    <blockquote class="note"><p><strong class="note">注意</strong>: 
     <span class="simpara">
      重申一次，在双引号字符串中，不给索引加上引号是合法的因此
      <code class="literal">&quot;$foo[bar]&quot;</code>
      是合法的（“合法”的原文为 valid。在实际测试中，这么做确实可以访问数组的该元素，但是会报一个常量未定义的
      notice。无论如何，强烈建议不要使用 $foo[bar]这样的写法，而要使用 $foo[&#039;bar&#039;]
      来访问数组中元素。--haohappy 注）。至于为什么参见以上的例子和
      <a href="language.types.string.html#language.types.string.parsing" class="link">字符串中的变量解析</a> 中的解释。
     </span>
    </p></blockquote>

   </div>
  </div>
 </div>

 <div class="sect2" id="language.types.array.casting">
  <h3 class="title">转换为数组</h3>
  
  <p class="para">
   对于任意 <span class="type">int</span>，<span class="type">float</span>，
   <span class="type">string</span>，<span class="type">bool</span> 和 <span class="type">resource</span> 类型，如果将一个值转换为
   <span class="type">array</span>，将得到一个仅有一个元素的数组，其下标为
   0，该元素即为此标量的值。换句话说，<code class="literal">(array)$scalarValue</code> 与
   <code class="literal">array($scalarValue)</code> 完全一样。
  </p>
  
  <p class="para">
   如果将 <span class="type">object</span> 类型转换为
   <span class="type">array</span>，则结果为一个数组，其单元为该对象的属性。键名将为成员变量名，不过有几点例外：整数属性不可访问；
   私有变量前会加上类名作前缀；保护变量前会加上一个 &#039;*&#039; 做前缀。这些前缀的前后都各有一个 <code class="literal">NUL</code> 字节。
   未初始化的<a href="language.oop5.properties.html#language.oop5.properties.typed-properties" class="link">类型属性</a>将会被丢弃。
  </p>

  <div class="informalexample">
   <div class="example-contents">
    <div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">A&nbsp;</span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;private&nbsp;</span><span style="color: #0000BB">$B</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;protected&nbsp;</span><span style="color: #0000BB">$C</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;</span><span style="color: #0000BB">$D</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;function&nbsp;</span><span style="color: #0000BB">__construct</span><span style="color: #007700">()<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;{</span><span style="color: #0000BB">1</span><span style="color: #007700">}&nbsp;=&nbsp;</span><span style="color: #0000BB">null</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /><br /></span><span style="color: #0000BB">var_export</span><span style="color: #007700">((array)&nbsp;new&nbsp;</span><span style="color: #0000BB">A</span><span style="color: #007700">());<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

   <p class="para">以上例程会输出：</p>
   <div class="example-contents screen">
    <div class="cdata"><pre>
array (
  &#039;&#039; . &quot;\0&quot; . &#039;A&#039; . &quot;\0&quot; . &#039;B&#039; =&gt; NULL,
  &#039;&#039; . &quot;\0&quot; . &#039;*&#039; . &quot;\0&quot; . &#039;C&#039; =&gt; NULL,
  &#039;D&#039; =&gt; NULL,
  1 =&gt; NULL,
)
</pre></div>
   </div>
  </div>

  <p class="para">
   这些 <code class="literal">NUL</code> 会导致一些意想不到的行为：
  </p>

  <div class="informalexample">
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">A&nbsp;</span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;private&nbsp;</span><span style="color: #0000BB">$A</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;将变为&nbsp;'\0A\0A'<br /></span><span style="color: #007700">}<br /><br />class&nbsp;</span><span style="color: #0000BB">B&nbsp;</span><span style="color: #007700">extends&nbsp;</span><span style="color: #0000BB">A&nbsp;</span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;private&nbsp;</span><span style="color: #0000BB">$A</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;将变为&nbsp;'\0B\0A'<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">public&nbsp;</span><span style="color: #0000BB">$AA</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;将变为&nbsp;'AA'<br /></span><span style="color: #007700">}<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">((array)&nbsp;new&nbsp;</span><span style="color: #0000BB">B</span><span style="color: #007700">());<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

   <p class="para">以上例程会输出：</p>
   <div class="example-contents screen">
    <div class="cdata"><pre>
array(3) {
  [&quot;BA&quot;]=&gt;
  NULL
  [&quot;AA&quot;]=&gt;
  NULL
  [&quot;AA&quot;]=&gt;
  NULL
}
</pre></div>
   </div>
  </div>

  <p class="para">
   上例会有两个键名为 &#039;AA&#039;，不过其中一个实际上是 &#039;\0A\0A&#039;。
  </p>
  
  <p class="para">
   将 <strong><code>null</code></strong> 转换为 <span class="type">array</span> 会得到一个空的数组。
  </p>
 </div>

 <div class="sect2" id="language.types.array.comparing">
  <h3 class="title">比较</h3>

  <p class="para">
   可以用 <span class="function"><a href="function.array-diff.html" class="function">array_diff()</a></span> 函数和
   <a href="language.operators.array.html" class="link">数组运算符</a> 来比较数组。
  </p>
 </div>

 <div class="sect2" id="language.types.array.unpacking">
  <h3 class="title">数组解包</h3>

  <p class="para">
   在 array 定义时，用 <code class="code">...</code> 前缀的一个 array 可以被展开到当前位置。
   只有实现了 <span class="interfacename"><a href="class.traversable.html" class="interfacename">Traversable</a></span> 的数组和对象才能被展开。
   PHP 7.4.0 开始可以使用 <code class="code">...</code> 解包 array。
  </p>

  <p class="para">
   它可以多次使用，在 <code class="code">...</code> 操作符前后都可以添加常规元素：
   <div class="example" id="example-62">
    <p><strong>示例 #9 简单的数组解包</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;使用简短的数组语法。<br />//&nbsp;亦可用于&nbsp;array()&nbsp;语法<br /></span><span style="color: #0000BB">$arr1&nbsp;</span><span style="color: #007700">=&nbsp;[</span><span style="color: #0000BB">1</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">3</span><span style="color: #007700">];<br /></span><span style="color: #0000BB">$arr2&nbsp;</span><span style="color: #007700">=&nbsp;[...</span><span style="color: #0000BB">$arr1</span><span style="color: #007700">];&nbsp;</span><span style="color: #FF8000">//[1,&nbsp;2,&nbsp;3]<br /></span><span style="color: #0000BB">$arr3&nbsp;</span><span style="color: #007700">=&nbsp;[</span><span style="color: #0000BB">0</span><span style="color: #007700">,&nbsp;...</span><span style="color: #0000BB">$arr1</span><span style="color: #007700">];&nbsp;</span><span style="color: #FF8000">//[0,&nbsp;1,&nbsp;2,&nbsp;3]<br /></span><span style="color: #0000BB">$arr4&nbsp;</span><span style="color: #007700">=&nbsp;[...</span><span style="color: #0000BB">$arr1</span><span style="color: #007700">,&nbsp;...</span><span style="color: #0000BB">$arr2</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">111</span><span style="color: #007700">];&nbsp;</span><span style="color: #FF8000">//[1,&nbsp;2,&nbsp;3,&nbsp;1,&nbsp;2,&nbsp;3,&nbsp;111]<br /></span><span style="color: #0000BB">$arr5&nbsp;</span><span style="color: #007700">=&nbsp;[...</span><span style="color: #0000BB">$arr1</span><span style="color: #007700">,&nbsp;...</span><span style="color: #0000BB">$arr1</span><span style="color: #007700">];&nbsp;</span><span style="color: #FF8000">//[1,&nbsp;2,&nbsp;3,&nbsp;1,&nbsp;2,&nbsp;3]<br /><br /></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">getArr</span><span style="color: #007700">()&nbsp;{<br />&nbsp;&nbsp;return&nbsp;[</span><span style="color: #DD0000">'a'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'b'</span><span style="color: #007700">];<br />}<br /></span><span style="color: #0000BB">$arr6&nbsp;</span><span style="color: #007700">=&nbsp;[...</span><span style="color: #0000BB">getArr</span><span style="color: #007700">(),&nbsp;</span><span style="color: #DD0000">'c'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'d'</span><span style="color: #007700">];&nbsp;</span><span style="color: #FF8000">//['a',&nbsp;'b',&nbsp;'c'&nbsp;=&gt;&nbsp;'d']<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>

  <p class="para">
   <code class="code">...</code> 操作符解包 array 时也遵守函数 <span class="function"><a href="function.array-merge.html" class="function">array_merge()</a></span> 的语义。
   也就是说，key 为字符时，后面的字符键会覆盖之前的字符键；key 为 integer 时则会重新编号：

   <div class="example" id="example-63">
    <p><strong>示例 #10 重复 key 的数组解包</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;string&nbsp;key<br /></span><span style="color: #0000BB">$arr1&nbsp;</span><span style="color: #007700">=&nbsp;[</span><span style="color: #DD0000">"a"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">];<br /></span><span style="color: #0000BB">$arr2&nbsp;</span><span style="color: #007700">=&nbsp;[</span><span style="color: #DD0000">"a"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">];<br /></span><span style="color: #0000BB">$arr3&nbsp;</span><span style="color: #007700">=&nbsp;[</span><span style="color: #DD0000">"a"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">,&nbsp;...</span><span style="color: #0000BB">$arr1</span><span style="color: #007700">,&nbsp;...</span><span style="color: #0000BB">$arr2</span><span style="color: #007700">];<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$arr3</span><span style="color: #007700">);&nbsp;</span><span style="color: #FF8000">//&nbsp;["a"&nbsp;=&gt;&nbsp;2]<br /><br />//&nbsp;integer&nbsp;key<br /></span><span style="color: #0000BB">$arr4&nbsp;</span><span style="color: #007700">=&nbsp;[</span><span style="color: #0000BB">1</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">3</span><span style="color: #007700">];<br /></span><span style="color: #0000BB">$arr5&nbsp;</span><span style="color: #007700">=&nbsp;[</span><span style="color: #0000BB">4</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">5</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">6</span><span style="color: #007700">];<br /></span><span style="color: #0000BB">$arr6&nbsp;</span><span style="color: #007700">=&nbsp;[...</span><span style="color: #0000BB">$arr4</span><span style="color: #007700">,&nbsp;...</span><span style="color: #0000BB">$arr5</span><span style="color: #007700">];<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$arr6</span><span style="color: #007700">);&nbsp;</span><span style="color: #FF8000">//&nbsp;[1,&nbsp;2,&nbsp;3,&nbsp;4,&nbsp;5,&nbsp;6]<br />//&nbsp;即&nbsp;[0&nbsp;=&gt;&nbsp;1,&nbsp;1&nbsp;=&gt;&nbsp;2,&nbsp;2&nbsp;=&gt;&nbsp;3,&nbsp;3&nbsp;=&gt;&nbsp;4,&nbsp;4&nbsp;=&gt;&nbsp;5,&nbsp;5&nbsp;=&gt;&nbsp;6]<br />//&nbsp;也就是原始的&nbsp;integer&nbsp;key&nbsp;不再保留<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>

  <blockquote class="note"><p><strong class="note">注意</strong>: 
   <p class="para">
    既不是 integer 也不是 string 的 key 会抛出 <span class="classname"><a href="class.typeerror.html" class="classname">TypeError</a></span>。
    这类 key 只能通过 <span class="interfacename"><a href="class.traversable.html" class="interfacename">Traversable</a></span> 对象生成。
   </p>
  </p></blockquote>
  <blockquote class="note"><p><strong class="note">注意</strong>: 
   <p class="para">
    在 PHP 8.1 之前，带有 string 键的 array 无法解包：
   </p>
   <div class="informalexample">
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br />$arr1&nbsp;</span><span style="color: #007700">=&nbsp;[</span><span style="color: #0000BB">1</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">3</span><span style="color: #007700">];<br /></span><span style="color: #0000BB">$arr2&nbsp;</span><span style="color: #007700">=&nbsp;[</span><span style="color: #DD0000">'a'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">4</span><span style="color: #007700">];<br /></span><span style="color: #0000BB">$arr3&nbsp;</span><span style="color: #007700">=&nbsp;[...</span><span style="color: #0000BB">$arr1</span><span style="color: #007700">,&nbsp;...</span><span style="color: #0000BB">$arr2</span><span style="color: #007700">];<br /></span><span style="color: #FF8000">//&nbsp;Fatal&nbsp;error:&nbsp;Uncaught&nbsp;Error:&nbsp;Cannot&nbsp;unpack&nbsp;array&nbsp;with&nbsp;string&nbsp;keys&nbsp;in&nbsp;example.php:5<br /><br /></span><span style="color: #0000BB">$arr4&nbsp;</span><span style="color: #007700">=&nbsp;[</span><span style="color: #0000BB">1</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">3</span><span style="color: #007700">];<br /></span><span style="color: #0000BB">$arr5&nbsp;</span><span style="color: #007700">=&nbsp;[</span><span style="color: #0000BB">4</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">5</span><span style="color: #007700">];<br /></span><span style="color: #0000BB">$arr6&nbsp;</span><span style="color: #007700">=&nbsp;[...</span><span style="color: #0000BB">$arr4</span><span style="color: #007700">,&nbsp;...</span><span style="color: #0000BB">$arr5</span><span style="color: #007700">];&nbsp;</span><span style="color: #FF8000">//&nbsp;works.&nbsp;[1,&nbsp;2,&nbsp;3,&nbsp;4,&nbsp;5]<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p></blockquote>

 </div>

 <div class="sect2" id="language.types.array.examples">
  <h3 class="title">示例</h3>

  <p class="para">
   PHP 中的数组类型有非常多的用途。以下是一些示例：
  </p>

  <div class="informalexample">
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;This:<br /></span><span style="color: #0000BB">$a&nbsp;</span><span style="color: #007700">=&nbsp;array(&nbsp;</span><span style="color: #DD0000">'color'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'red'</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'taste'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'sweet'</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'shape'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'round'</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'name'&nbsp;&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'apple'</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">4&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;键名为&nbsp;0<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$b&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #DD0000">'a'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'b'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'c'</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;.&nbsp;.&nbsp;.完全等同于:<br /></span><span style="color: #0000BB">$a&nbsp;</span><span style="color: #007700">=&nbsp;array();<br /></span><span style="color: #0000BB">$a</span><span style="color: #007700">[</span><span style="color: #DD0000">'color'</span><span style="color: #007700">]&nbsp;=&nbsp;</span><span style="color: #DD0000">'red'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$a</span><span style="color: #007700">[</span><span style="color: #DD0000">'taste'</span><span style="color: #007700">]&nbsp;=&nbsp;</span><span style="color: #DD0000">'sweet'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$a</span><span style="color: #007700">[</span><span style="color: #DD0000">'shape'</span><span style="color: #007700">]&nbsp;=&nbsp;</span><span style="color: #DD0000">'round'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$a</span><span style="color: #007700">[</span><span style="color: #DD0000">'name'</span><span style="color: #007700">]&nbsp;&nbsp;=&nbsp;</span><span style="color: #DD0000">'apple'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$a</span><span style="color: #007700">[]&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=&nbsp;</span><span style="color: #0000BB">4</span><span style="color: #007700">;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;键名为&nbsp;0<br /><br /></span><span style="color: #0000BB">$b&nbsp;</span><span style="color: #007700">=&nbsp;array();<br /></span><span style="color: #0000BB">$b</span><span style="color: #007700">[]&nbsp;=&nbsp;</span><span style="color: #DD0000">'a'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$b</span><span style="color: #007700">[]&nbsp;=&nbsp;</span><span style="color: #DD0000">'b'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$b</span><span style="color: #007700">[]&nbsp;=&nbsp;</span><span style="color: #DD0000">'c'</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">//&nbsp;执行上述代码后，数组&nbsp;$a&nbsp;将是<br />//&nbsp;array('color'&nbsp;=&gt;&nbsp;'red',&nbsp;'taste'&nbsp;=&gt;&nbsp;'sweet',&nbsp;'shape'&nbsp;=&gt;&nbsp;'round',&nbsp;<br />//&nbsp;'name'&nbsp;=&gt;&nbsp;'apple',&nbsp;0&nbsp;=&gt;&nbsp;4)，&nbsp;数组&nbsp;$b&nbsp;将是<br />//&nbsp;array(0&nbsp;=&gt;&nbsp;'a',&nbsp;1&nbsp;=&gt;&nbsp;'b',&nbsp;2&nbsp;=&gt;&nbsp;'c'),&nbsp;或简单的&nbsp;array('a',&nbsp;'b',&nbsp;'c').<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

  </div>

  <div class="example" id="example-64">
   <p><strong>示例 #11 使用 array()</strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;Array&nbsp;as&nbsp;(property-)map<br /></span><span style="color: #0000BB">$map&nbsp;</span><span style="color: #007700">=&nbsp;array(&nbsp;</span><span style="color: #DD0000">'version'&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">4</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'OS'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'Linux'</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'lang'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'english'</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'short_tags'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">true<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br /></span><span style="color: #FF8000">//&nbsp;严格的数字键<br /></span><span style="color: #0000BB">$array&nbsp;</span><span style="color: #007700">=&nbsp;array(&nbsp;</span><span style="color: #0000BB">7</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">8</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">156</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-</span><span style="color: #0000BB">10<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">);<br /></span><span style="color: #FF8000">//&nbsp;这相当于&nbsp;array(0&nbsp;=&gt;&nbsp;7,&nbsp;1&nbsp;=&gt;&nbsp;8,&nbsp;...)<br /><br /></span><span style="color: #0000BB">$switching&nbsp;</span><span style="color: #007700">=&nbsp;array(&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">10</span><span style="color: #007700">,&nbsp;</span><span style="color: #FF8000">//&nbsp;key&nbsp;=&nbsp;0<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">5&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">=&gt;&nbsp;&nbsp;</span><span style="color: #0000BB">6</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">3&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">=&gt;&nbsp;&nbsp;</span><span style="color: #0000BB">7</span><span style="color: #007700">,&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'a'&nbsp;&nbsp;</span><span style="color: #007700">=&gt;&nbsp;&nbsp;</span><span style="color: #0000BB">4</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">11</span><span style="color: #007700">,&nbsp;</span><span style="color: #FF8000">//&nbsp;key&nbsp;=&nbsp;6&nbsp;(整数索引的最大值为&nbsp;5)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'8'&nbsp;&nbsp;</span><span style="color: #007700">=&gt;&nbsp;&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">,&nbsp;</span><span style="color: #FF8000">//&nbsp;key&nbsp;=&nbsp;8&nbsp;(整数！)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'02'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">77</span><span style="color: #007700">,&nbsp;</span><span style="color: #FF8000">//&nbsp;key&nbsp;=&nbsp;'02'<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">0&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">12&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;值&nbsp;10&nbsp;被&nbsp;12&nbsp;覆盖<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br /></span><span style="color: #FF8000">//&nbsp;空数组<br /></span><span style="color: #0000BB">$empty&nbsp;</span><span style="color: #007700">=&nbsp;array();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>

   </div>

  </div>

  <div class="example" id="language.types.array.examples.loop">
   <p><strong>示例 #12 集合</strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$colors&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #DD0000">'red'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'blue'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'green'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'yellow'</span><span style="color: #007700">);<br /><br />foreach&nbsp;(</span><span style="color: #0000BB">$colors&nbsp;</span><span style="color: #007700">as&nbsp;</span><span style="color: #0000BB">$color</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"Do&nbsp;you&nbsp;like&nbsp;</span><span style="color: #0000BB">$color</span><span style="color: #DD0000">?\n"</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

   <div class="example-contents"><p>以上例程会输出：</p></div>
   <div class="example-contents screen">
<div class="cdata"><pre>
Do you like red?
Do you like blue?
Do you like green?
Do you like yellow?
</pre></div>
   </div>
  </div>
  
  <p class="para">
   可以通过引用传递 <span class="type">array</span> 的值来直接更改数组的值。
  </p>

  <div class="example" id="language.types.array.examples.changeloop">
   <p><strong>示例 #13 在循环中改变单元</strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">foreach&nbsp;(</span><span style="color: #0000BB">$colors&nbsp;</span><span style="color: #007700">as&nbsp;&amp;</span><span style="color: #0000BB">$color</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$color&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">strtoupper</span><span style="color: #007700">(</span><span style="color: #0000BB">$color</span><span style="color: #007700">);<br />}<br />unset(</span><span style="color: #0000BB">$color</span><span style="color: #007700">);&nbsp;</span><span style="color: #FF8000">/*&nbsp;确保后面对<br />$color&nbsp;的写入不会修改最后一个数组元素&nbsp;*/<br /><br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$colors</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

   <div class="example-contents"><p>以上例程会输出：</p></div>
   <div class="example-contents screen">
<div class="cdata"><pre>
Array
(
    [0] =&gt; RED
    [1] =&gt; BLUE
    [2] =&gt; GREEN
    [3] =&gt; YELLOW
)
</pre></div>
   </div>
  </div>

  <p class="para">
   本例生成一个下标从 1 开始的数组。
  </p>

  <div class="example" id="example-67">
   <p><strong>示例 #14 下标从 1 开始的数组</strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$firstquarter&nbsp;&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #0000BB">1&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'January'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'February'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'March'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$firstquarter</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

   <div class="example-contents"><p>以上例程会输出：</p></div>
   <div class="example-contents screen">
<div class="cdata"><pre>
Array 
(
    [1] =&gt; &#039;January&#039;
    [2] =&gt; &#039;February&#039;
    [3] =&gt; &#039;March&#039;
)
</pre></div>   
   </div>
  </div>

  <div class="example" id="example-68">
   <p><strong>示例 #15 填充数组</strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;把指定目录中的所有项填充到数组<br /></span><span style="color: #0000BB">$handle&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">opendir</span><span style="color: #007700">(</span><span style="color: #DD0000">'.'</span><span style="color: #007700">);<br />while&nbsp;(</span><span style="color: #0000BB">false&nbsp;</span><span style="color: #007700">!==&nbsp;(</span><span style="color: #0000BB">$file&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">readdir</span><span style="color: #007700">(</span><span style="color: #0000BB">$handle</span><span style="color: #007700">)))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$files</span><span style="color: #007700">[]&nbsp;=&nbsp;</span><span style="color: #0000BB">$file</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">closedir</span><span style="color: #007700">(</span><span style="color: #0000BB">$handle</span><span style="color: #007700">);&nbsp;<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

  </div>

  <p class="para">
   <span class="type">Array</span> 是有序的。也可以使用不同的排序函数来改变顺序。更多信息参见
   <a href="ref.array.html" class="link">数组函数</a>。可以用
   <span class="function"><a href="function.count.html" class="function">count()</a></span> 函数来统计出
   <span class="type">array</span> 中元素的个数。
  </p>

  <div class="example" id="example-69">
   <p><strong>示例 #16 数组排序</strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />sort</span><span style="color: #007700">(</span><span style="color: #0000BB">$files</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$files</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

  </div>

  <p class="para">
   因为 <span class="type">array</span> 中的值可以为任意值，也可是另一个
   <span class="type">array</span>。这样可以产生递归或多维 <span class="type">array</span>。
  </p>

  <div class="example" id="example-70">
   <p><strong>示例 #17 递归和多维数组</strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$fruits&nbsp;</span><span style="color: #007700">=&nbsp;array&nbsp;(&nbsp;</span><span style="color: #DD0000">"fruits"&nbsp;&nbsp;</span><span style="color: #007700">=&gt;&nbsp;array&nbsp;(&nbsp;</span><span style="color: #DD0000">"a"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"orange"</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"b"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"banana"</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"c"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"apple"<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">),<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"numbers"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;array&nbsp;(&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">3</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">4</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">5</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">6<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">),<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"holes"&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">=&gt;&nbsp;array&nbsp;(&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"first"</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">5&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"second"</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"third"<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;);<br /><br /></span><span style="color: #FF8000">//&nbsp;处理上面数组中的值的一些例子<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">$fruits</span><span style="color: #007700">[</span><span style="color: #DD0000">"holes"</span><span style="color: #007700">][</span><span style="color: #0000BB">5</span><span style="color: #007700">];&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;打印&nbsp;"second"<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">$fruits</span><span style="color: #007700">[</span><span style="color: #DD0000">"fruits"</span><span style="color: #007700">][</span><span style="color: #DD0000">"a"</span><span style="color: #007700">];&nbsp;</span><span style="color: #FF8000">//&nbsp;打印&nbsp;"orange"<br /></span><span style="color: #007700">unset(</span><span style="color: #0000BB">$fruits</span><span style="color: #007700">[</span><span style="color: #DD0000">"holes"</span><span style="color: #007700">][</span><span style="color: #0000BB">0</span><span style="color: #007700">]);&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;删除&nbsp;"first"<br /><br />//&nbsp;创建一个新的多维数组<br /></span><span style="color: #0000BB">$juices</span><span style="color: #007700">[</span><span style="color: #DD0000">"apple"</span><span style="color: #007700">][</span><span style="color: #DD0000">"green"</span><span style="color: #007700">]&nbsp;=&nbsp;</span><span style="color: #DD0000">"good"</span><span style="color: #007700">;&nbsp;<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

  </div>

  <p class="para">
   <span class="type">Array</span> 的赋值总是会涉及到值的拷贝。使用
   <a href="language.operators.html" class="link">引用运算符</a> 通过引用来拷贝
   <span class="type">array</span>。
  </p>

  <div class="informalexample">
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$arr1&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #0000BB">2</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">3</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$arr2&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$arr1</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$arr2</span><span style="color: #007700">[]&nbsp;=&nbsp;</span><span style="color: #0000BB">4</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;$arr2&nbsp;已更改，<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;$arr1&nbsp;仍然是&nbsp;array(2,&nbsp;3)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br /></span><span style="color: #0000BB">$arr3&nbsp;</span><span style="color: #007700">=&nbsp;&amp;</span><span style="color: #0000BB">$arr1</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$arr3</span><span style="color: #007700">[]&nbsp;=&nbsp;</span><span style="color: #0000BB">4</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;现在&nbsp;$arr1&nbsp;和&nbsp;$arr3&nbsp;是一样的<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

  </div>

 </div>
</div></div></div></body></html>